/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.pa165languageschoolclient;

import com.pa165.languageschoolservice.DTO.CourseDTO;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.List;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Elena Medvedeva
 */
public class ClientCourse {

    public static final String COURSES = "courses";
    public static final String GET_ALL_COURSES = "getAllCourses";
    public static final String GET_COURSE = "getCourse";
    public static final String GET_COURSE_BY_NAME = "getCoursesByName";
    public static final String ADD_COURSE = "createCourse";
    public static final String EDIT_COURSE = "updateCourse";
    public static final String DELETE_COURSE = "deleteCourse";
    
    private Client client;
    private String url;   

    public ClientCourse(String url) {
        client = AppClient.getClient();
        this.url = url;        
    }

    public String getCourseURL() {
        return url + COURSES;
    }

    public List<CourseDTO> getAllCourses() {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getCourseURL() + "/" + GET_ALL_COURSES 
                + CommandLineClient.USER_PASSWORD
                );
        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

        if (response.getStatus() != 200) {
            System.out.println("Request  failed: " + response.getEntity(String.class));
            return null;
        }
        GenericType<List<CourseDTO>> type = new GenericType<List<CourseDTO>>() {
        };
        List<CourseDTO> output = response.getEntity(type);
        return output;
    }

    public List<CourseDTO> findByName(String name) {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getCourseURL() + "/" + GET_COURSE_BY_NAME + "?name=" + name);

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

        if (response.getStatus() != 200) {
            System.out.println("Request  failed: " + response.getEntity(String.class));
            return null;
        }
        GenericType<List<CourseDTO>> type = new GenericType<List<CourseDTO>>() {
        };
        List<CourseDTO> output = response.getEntity(type);
        return output;
    }

    public CourseDTO getCourse(int id) {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webRes = client
                .resource(getCourseURL() + "/" + GET_COURSE + "?id=" + id);

        ClientResponse response = webRes.accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);
       
        if (response.getStatus() != 200) {
            if (response.getStatus() == 404) {
                System.out.println("Couldn't find course with id=" + id);

            } else {
                System.out.println("Request  failed: " + response.getEntity(String.class));
            }
            return null;
        }
        CourseDTO output = response.getEntity(CourseDTO.class);
        return output;
    }

    public boolean deleteCourse(int id) {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webRes = client
                .resource(getCourseURL() + "/" + DELETE_COURSE + "?id=" + id);

        ClientResponse response = webRes.accept(MediaType.APPLICATION_JSON)
                .delete(ClientResponse.class);

        int status = response.getStatus();

        if (status == 200) {
            return true;
        } else {
            if (status == 404) {
                System.out.println("Couldn't find course with id=" + id);
            } else {
                System.out.println("Request  failed: " + response.getEntity(String.class));
            }            
            return false;
        }
    }

    public boolean addCourse(CourseDTO course) {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getCourseURL() + "/" + ADD_COURSE);

        ClientResponse cr = webResource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, course);
        int status = cr.getStatus();
        if ((status >= 200) && (status <= 299)) {
            System.out.println("status=: " + status);
            return true;
        } else if (status == 500) {
            System.out.println("Trying to add course with name " + course.getName() + ", which already exists");
            return false;
        } else {
            System.out.println("Request  failed: " + cr.getEntity(String.class));
            return false;
        }
    }

    public boolean editCourse(CourseDTO course) {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getCourseURL() + "/" + EDIT_COURSE);

        ClientResponse cr = webResource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, course);
        int status = cr.getStatus();
        if ((status >= 200) && (status <= 299)) {
            System.out.println("status=: " + status);
            return true;
        } else if (status == 500) {
            System.out.println("Trying to modify course with name " + course.getName() + ", which already exists");
            return false;
        } else {
            System.out.println("Request  failed: " + cr.getEntity(String.class));
            return false;
        }
    }
}
